#!/usr/bin/env Jython
#-*- coding:utf-8 -*-

from javax.swing import *
from java.awt import *
import jythonCollectData
import csvjython
	
class Example(JFrame):

        
	def evt_execute(self, event):
		self.result = jythonCollectData.getQuery(self.ComboBox1, self.ComboBox2, self.ComboBox3, self.ComboBox4)
		self.label2.setText(str(len(self.result)))
	
	def checked1(self, event):
		if self.chkbox1.isSelected():
			self.ComboBox1.setEnabled(True)
		else:
			self.ComboBox1.setEnabled(False)
			
	def checked2(self, event):
		if self.chkbox2.isSelected():
			self.ComboBox2.setEnabled(True)
		else:
			self.ComboBox2.setEnabled(False)
			
	def checked3(self, event):
		if self.chkbox3.isSelected():
			self.ComboBox3.setEnabled(True)
		else:
			self.ComboBox3.setEnabled(False)
			
	def checked4(self, event):
		if self.chkbox4.isSelected():
			self.ComboBox4.setEnabled(True)
		else:
			self.ComboBox4.setEnabled(False)
			
	def evt_csvtodb(self, event):
		csvjython.main()
		
	def __init__(self):
		
		layout = GroupLayout(self.getContentPane())
		self.getContentPane().setLayout(layout)
		layout.setAutoCreateGaps(True)
		layout.setAutoCreateContainerGaps(True)

		self.setPreferredSize(Dimension(1100, 280))
        
		self.chkbox1 = JCheckBox("establishments", actionPerformed=self.checked1)
		self.chkbox2 = JCheckBox("category", actionPerformed=self.checked2)
		self.chkbox3 = JCheckBox("degree", actionPerformed=self.checked3)
		self.chkbox4 = JCheckBox("year", actionPerformed=self.checked4)
		
		self.ComboBox1 = JComboBox(jythonCollectData.getEstablishment())
		self.ComboBox2 = JComboBox(jythonCollectData.getCategories())
		self.ComboBox3 = JComboBox(jythonCollectData.getDegrees())
		self.ComboBox4 = JComboBox(jythonCollectData.getYears())
		
		self.ComboBox1.setEnabled(False)		
		self.ComboBox2.setEnabled(False)
		self.ComboBox3.setEnabled(False)
		self.ComboBox4.setEnabled(False)
		
		self.button1 = JButton("Execute", actionPerformed=self.evt_execute)
		self.button2 = JButton("Generate html")
		self.button3 = JButton("CSV to DB", actionPerformed=self.evt_csvtodb)
		self.button4 = JButton("Generate CSV")
		self.label1 = JLabel("Result:")
		self.label2 = JLabel("")
        

		layout.setHorizontalGroup(layout.createSequentialGroup()
			.addGroup(layout.createParallelGroup()
				.addComponent(self.chkbox1)
				.addComponent(self.chkbox2)
				.addComponent(self.chkbox3)
				.addComponent(self.chkbox4)
				.addComponent(self.button1)
				.addComponent(self.button3)
				.addComponent(self.label1))
			.addGroup(layout.createParallelGroup()
				.addComponent(self.ComboBox1)
				.addComponent(self.ComboBox2)
				.addComponent(self.ComboBox3)
				.addComponent(self.ComboBox4)
				.addComponent(self.button2)
				.addComponent(self.button4)
				.addComponent(self.label2))
		)
		
		layout.setVerticalGroup(layout.createSequentialGroup()
			.addGroup(layout.createParallelGroup()
				.addComponent(self.chkbox1)
				.addComponent(self.ComboBox1))
				
			.addGroup(layout.createParallelGroup()
				.addComponent(self.chkbox2)
				.addComponent(self.ComboBox2))
				
			.addGroup(layout.createParallelGroup()
				.addComponent(self.chkbox3)
				.addComponent(self.ComboBox3))
			
			.addGroup(layout.createParallelGroup()
				.addComponent(self.chkbox4)
				.addComponent(self.ComboBox4))
				
			.addGroup(layout.createParallelGroup()
				.addComponent(self.button1)
				.addComponent(self.button2))
				
			.addGroup(layout.createParallelGroup()
				.addComponent(self.button3)
				.addComponent(self.button4))
				
			.addGroup(layout.createParallelGroup()
				.addComponent(self.label1)
				.addComponent(self.label2)))

		self.pack()

		self.setTitle("Rebides")
		self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
		self.setLocationRelativeTo(None)
		self.setVisible(True)


if __name__ == '__main__':
    Example()
